package com.common.service.judicialpersonal.impl;

import com.common.constants.Constants;
import com.common.converter.BusinessJobExperienceConverter;
import com.common.converter.JobChangeExperienceConverter;
import com.common.converter.JobExplainConverter;
import com.common.entity.department.QDepartmentEntity;
import com.common.entity.job.JobExplainEntity;
import com.common.entity.job.QJobEntity;
import com.common.entity.job.QJobExplainEntity;
import com.common.entity.personal.BusinessJobExperienceEntity;
import com.common.entity.personal.JobChangeExperienceEntity;
import com.common.repository.judicialpersonal.BusinessJobExperienceRepository;
import com.common.repository.judicialpersonal.JobChangeExperienceRepository;
import com.common.service.judicialpersonal.JobExperienceService;
import com.common.vo.judicialpersonal.BusinessJobExperienceVo;
import com.common.vo.judicialpersonal.JobChangeExperienceVo;
import com.common.vo.judicialpersonal.JobExplainVo;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务岗位经历 Service
 *
 * @author johnny
 * @create 2021-05-13 10:21 下午
 **/
@Service
@Slf4j
@Transactional(rollbackOn = Exception.class)
public class JobExperienceServiceImpl implements JobExperienceService {


    private final BusinessJobExperienceRepository businessJobExperienceRepository;
    private final JobChangeExperienceRepository jobChangeExperienceRepository;


    private final JPAQueryFactory queryFactory;

    public JobExperienceServiceImpl(BusinessJobExperienceRepository businessJobExperienceRepository, JobChangeExperienceRepository jobChangeExperienceRepository, JPAQueryFactory queryFactory) {
        this.businessJobExperienceRepository = businessJobExperienceRepository;
        this.jobChangeExperienceRepository = jobChangeExperienceRepository;
        this.queryFactory = queryFactory;
    }

    /**
     * 根据 个人档案ID 查询 业务岗位经历
     *
     * @param personalId
     * @param order
     * @return
     */
    @Override
    public List<BusinessJobExperienceVo> findBusinessJobExperiencesByPersonalId(Long personalId, String order) {
        //默认 按照 experienceStartTime 降序
        if (StringUtils.isEmpty(order)) {
            order = Constants.DESC;
        }
        List<BusinessJobExperienceVo> list = new ArrayList<>();
        List<BusinessJobExperienceEntity> businessJobExperienceEntityList = null;
        log.info("【findBusinessJobExperiencesByPersonalId start】");
        if (Constants.ASC.equals(order)) {
            businessJobExperienceEntityList = businessJobExperienceRepository.findByPersonalIdOrderByExperienceStartTimeAsc(personalId);
        } else if (Constants.DESC.equals(order)) {
            businessJobExperienceEntityList = businessJobExperienceRepository.findByPersonalIdOrderByExperienceStartTimeDesc(personalId);
        }
        if (CollectionUtils.isNotEmpty(businessJobExperienceEntityList)) {
            businessJobExperienceEntityList.forEach(businessJobExperience -> {
                BusinessJobExperienceVo businessJobExperienceVo = BusinessJobExperienceConverter.INSTANCE.domain2vo(businessJobExperience);
                list.add(businessJobExperienceVo);
            });
        }
        log.info("【findBusinessJobExperiencesByPersonalId end】");
        return list;
    }

    /**
     * 根据 个人档案ID 查询 业务岗位经历
     *
     * @param personalId
     * @param order
     * @return
     */
    @Override
    public List<JobChangeExperienceVo> findJobChangeExperienceByPersonalId(Long personalId, String order) {
        //默认 按照 experienceStartTime 降序
        if (StringUtils.isEmpty(order)) {
            order = Constants.DESC;
        }
        List<JobChangeExperienceVo> list = new ArrayList<>();
        List<JobChangeExperienceEntity> jobChangeExperienceEntityList = null;
        log.info("【findJobChangeExperienceByPersonalId start】");
        if (Constants.ASC.equals(order)) {
            jobChangeExperienceEntityList = jobChangeExperienceRepository.findByPersonalIdOrderByTimeToChangeAsc(personalId);
        } else if (Constants.DESC.equals(order)) {
            jobChangeExperienceEntityList = jobChangeExperienceRepository.findByPersonalIdOrderByTimeToChangeDesc(personalId);
        }
        if (CollectionUtils.isNotEmpty(jobChangeExperienceEntityList)) {
            jobChangeExperienceEntityList.forEach(jobChangeExperienceEntity -> {
                JobChangeExperienceVo jobChangeExperienceVo = JobChangeExperienceConverter.INSTANCE.domain2vo(jobChangeExperienceEntity);
                list.add(jobChangeExperienceVo);
            });
        }
        log.info("【findJobChangeExperienceByPersonalId end】");
        return list;
    }

    /**
     * 根据 个人档案ID 查询 岗位说明书
     *
     * @param personalId
     * @return : JobExplainVo
     */
    @Override
    public JobExplainVo findJobExperience(Long personalId) {
        JobExplainVo jobExplainVo = new JobExplainVo();
        QJobExplainEntity qJobExplainEntity = QJobExplainEntity.jobExplainEntity;
        QJobEntity qJobEntity = QJobEntity.jobEntity;
        QDepartmentEntity qDepartmentEntity = QDepartmentEntity.departmentEntity;

        Tuple tuple = queryFactory.select(qJobExplainEntity, qJobEntity.jobName, qDepartmentEntity.departmentName)
                .from(qJobExplainEntity)
                .leftJoin(qJobEntity).on(qJobEntity.id.eq(qJobExplainEntity.jobId))
                .leftJoin(qDepartmentEntity).on(qDepartmentEntity.id.eq(qJobExplainEntity.departmentId))
                .where(qJobExplainEntity.personalId.eq(personalId))
                .fetchOne();

        if (tuple != null) {
            JobExplainEntity jobExplainEntity = tuple.get(0, JobExplainEntity.class);
            String jobName = tuple.get(1, String.class);
            String departmentName = tuple.get(2, String.class);
            jobExplainVo = JobExplainConverter.INSTANCE.domain2vo(jobExplainEntity);
            jobExplainVo.setJobName(jobName);
            jobExplainVo.setDepartmentName(departmentName);
        }
        return jobExplainVo;
    }
}